package com.zking.frame;

import com.zking.dao.EvaluateDao;
import com.zking.dao.OrderDao;
import com.zking.entity.Address;
import com.zking.entity.Evaluate;
import com.zking.entity.Order;
import com.zking.utils.Tool工具;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

public class EvaluateFrame extends JDialog {
    public EvaluateFrame(JFrame owner, boolean modal, String name, String shop, Order order) {
        super(owner, modal);
        this.setBounds(owner.getX() + 15, owner.getY() + 220, 600, 700);
        this.setResizable(false);// 设置窗口不可调整大小
        this.setTitle(name);
        this.setLayout(null);
        JLabel jLabelshopname = new JLabel(shop,0);
        jLabelshopname.setBounds(0,10,600,50);
        jLabelshopname.setFont(new Font("黑体", Font.BOLD,30));
        jLabelshopname.setOpaque(true);
        add(jLabelshopname);

        JLabel jLabelContent = new JLabel("评价",0);
        jLabelContent.setBounds(20,80,100,50);
        jLabelContent.setFont(new Font("黑体",Font.BOLD,30));
        jLabelContent.setOpaque(true);
        add(jLabelContent);

        TextArea textArea = new TextArea();
        textArea.setBounds(130,80,450,200);
        textArea.setFont(new Font("黑体",Font.BOLD,20));
        add(textArea);

        JLabel jLabelStar = new JLabel("星级",0);
        jLabelStar.setBounds(20,300,100,50);
        jLabelStar.setFont(new Font("黑体", Font.BOLD,30));
        jLabelStar.setOpaque(true);
        add(jLabelStar);

        String[] array = {"★","★★","★★★","★★★★","★★★★★"};
        JComboBox jComboStar = new JComboBox(array);
        jComboStar.setBounds(130,300,250,50);
        jComboStar.setFont(new Font("黑体",Font.BOLD,30));
        jComboStar.setBackground(Color.white);
        add(jComboStar);

        JButton jButtonSubmit =  new JButton("提交评价");
        jButtonSubmit.setBounds(225,400,150,50);
        jButtonSubmit.setFont(new Font("黑体",Font.BOLD,20));
        add(jButtonSubmit);

        jButtonSubmit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                Evaluate evaluate = new Evaluate();
                evaluate.setContent(textArea.getText());
                evaluate.setShopname(shop);
                Tool工具 tool工具 = new Tool工具();
                evaluate.setUsername(tool工具.getName());
                evaluate.setStar(jComboStar.getSelectedItem().toString());
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                String time = df.format(new Date());
                evaluate.setTime(time);
                EvaluateDao evaluateDao = new EvaluateDao();
                int a = evaluateDao.insertEvaluate(evaluate);
                if(a==1){
                    OrderDao orderDao = new OrderDao();
                    orderDao.updateOrder("T",order);
                    JOptionPane.showMessageDialog(owner, "提交成功", "成功", JOptionPane.OK_CANCEL_OPTION);
                    dispose();
                }else {
                    JOptionPane.showMessageDialog(owner, "提交失败", "失败", JOptionPane.OK_CANCEL_OPTION);
                }

            }
        });
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    }

}
